<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>DawnGB</title>
    <script src="https://cdn.jsdelivr.net/gh/golang/go@go1.21.5/misc/wasm/wasm_exec.js"></script>
  </head>

  <body>
    <script>
      (async () => {
        const resp = await fetch("core.wasm");
        const go = new Go();
        const buffer = await resp.arrayBuffer();
        const result = await WebAssembly.instantiate(buffer, go.importObject);
        go.run(result.instance);

        window.addEventListener("message", (e) => {
          if (
            e.origin.includes("localhost") ||
            e.origin.includes("dawngb") ||
            e.origin.includes("akatsuki105")
          ) {
            switch (e.data[0]) {
              case "reset": {
                globalThis.reset();
                break;
              }
              case "pause": {
                globalThis.setPaused(e.data[1]);
                break;
              }
              case "sound": {
                globalThis.sound(e.data[1]);
                break;
              }
              case "press": {
                globalThis.press(e.data[1], e.data[2]);
                break;
              }
              case "rom": {
                switch (e.data[1]) {
                  case "load":
                    const rom = e.data[2]; // Uint8Array
                    if (rom.length > 0) {
                      globalThis.loadROM(rom);
                    }
                    break;
                }
                break;
              }
              case "save": {
                switch (e.data[1]) {
                  case "load":
                    const save = e.data[2]; // Uint8Array
                    if (save.length > 0) {
                      globalThis.loadSave(save);
                    }
                    break;
                  case "dump":
                    const sram = globalThis.dumpSave(); // Uint8Array
                    if (sram.length > 0) {
                      // download file
                      const blob = new Blob([sram], {
                        type: "application/octet-stream",
                      });
                      const url = URL.createObjectURL(blob);
                      const a = document.createElement("a");
                      a.href = url;
                      a.download = "dawngb.sav";
                      a.click();
                      a.remove();
                    }
                    break;
                }
                break;
              }
              case "screenshot": {
                const canvas = document.querySelector("canvas");
                const data = canvas.toDataURL("image/png");
                const a = document.createElement("a");
                a.href = data;
                a.download = "dawngb.png";
                a.click();
                a.remove();
                break;
              }
              default: {
                console.log(`Unknown message: ${e.data}`);
                break;
              }
            }
          }
        });
      })();
    </script>
  </body>
</html>
